package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地址簿业务层
 * @author Xiao
 * @date 2021/9/29
 */
@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    /**
     * 查询所有地址数据
     * @return
     */
    @Override
    public List<AddressBook> addressListApi() {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(AddressBook::getCreateTime);
        return this.list(wrapper);
    }
    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @Override
    public boolean addAddressApi(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        return this.save(addressBook);
    }
    /**
     * 获取默认地址
     * @return
     */
    @Override
    public AddressBook getDefaultAddressApi() {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault, 1);
        return this.getOne(wrapper);
    }
    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @Override
    public boolean setDefaultAddressApi(AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault, 0);
        addressBook.setIsDefault(1);
        return this.update(wrapper) && this.updateById(addressBook);
        /*AddressBook defaultAddressBook = this.getDefaultAddressApi();
        defaultAddressBook.setIsDefault(0);
        addressBook.setIsDefault(1);
        return this.updateById(defaultAddressBook) && this.updateById(addressBook);*/
    }
    /**
     * 根据id查询单个地址
     * @param id
     * @return
     */
    @Override
    public AddressBook addressFindOneApi(Long id) {
        return this.getById(id);
    }
    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @Override
    public boolean updateAddressApi(AddressBook addressBook) {
        return this.updateById(addressBook);
    }
    /**
     * 根据id删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteAddressApi(Long ids) {
        return this.removeById(ids);
    }
    /**
     * 查询最新地址
     * @return
     */
    @Override
    public AddressBook addressLastUpdateApi() {
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",BaseContext.getCurrentId());
        wrapper.select("*, max(update_time) max");
        return this.getOne(wrapper);
    }
}
